/* 
 * All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved. 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 
 * use this file except in compliance with the License. You may obtain a copy 
 * of the License at 
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0 
 *   
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
 * License for the specific language governing permissions and limitations 
 * under the License.
 * 
 */
 
package JAVA300.第十一章多线程;

import static org.quartz.DateBuilder.evenSecondDateAfterNow;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Date;

/**
 * 官网自带demo-SimpleExample--Quartz1是任务类
 * 需要导入源文件里的log4j 到src下面

 * 
 * @author Bill Kratzer
 */
public class thread_19_others_2Quartz2 {

  public void run() throws Exception {


    // First we must get a reference to a scheduler
    //1.创建Scheduler的工厂
    SchedulerFactory sf = new StdSchedulerFactory();
    //2.从工厂中获得调度器
    Scheduler sched = sf.getScheduler();

    //3.创建JobDetail--withDetail唯一标识
    JobDetail job = newJob(thread_19_others_2Quartz1.class).withIdentity("job1", "group1").build();

    // computer a time that is on the next round minute
    //时间--evenM。。为静态方法 还有evevSecond，，，
    Date runTime = evenSecondDateAfterNow();

    // Trigger the job to run on the next round minute
    //4.触发器  startAt从什么时候开始
    //Trigger trigger = newTrigger().withIdentity("trigger1", "group1").startAt(runTime).build();

    //也可以定时定量弹出
    Trigger trigger = newTrigger().withIdentity("trigger1", "group1").startAt(runTime)
                         .withSchedule(simpleSchedule().withIntervalInSeconds(5).withRepeatCount(3)).build();

    // Tell quartz to schedule the job using our trigger
    //5.注册任务和触发条件
    sched.scheduleJob(job, trigger);

    // Start up the scheduler (nothing can actually run until the
    // scheduler has been started)
    //6.启动
    sched.start();

    try {
      // wait 65 seconds to show job
      Thread.sleep(50000L * 1000L);//65s之后 shutdown
      // executing...
    } catch (Exception e) {
      //
    }
    sched.shutdown(true);

  }

  public static void main(String[] args) throws Exception {

    thread_19_others_2Quartz2 example = new thread_19_others_2Quartz2();
    example.run();

  }

}
